/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-21
*/
package com.rzico.account.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.entity.SysEmployee;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysEmployeeService;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.account.entity.EnterpriseDeposit;
import com.rzico.account.service.EnterpriseDepositService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 名厨档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-21
 */
@Api(description = "名厨档案接口")
@RestController
@RequestMapping("/admin/enterpriseDeposit")
public class EnterpriseDepositController extends BaseController {

    @Autowired
    private EnterpriseDepositService enterpriseDepositService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysEmployeeService sysEmployeeService;

    /**
     * 分页查询名厨档案
     *
     * @return
    */
    @ApiOperation("分页查询名厨档案")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseId", value = "商家ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "type", value = "账单类型", dataType = "String", paramType = "query")
    })
    public CommResult<EnterpriseDeposit> list(Long enterpriseId,Date startDate, Date endDate,Integer type, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (enterpriseId!=null){
            params.put("enterpriseId", enterpriseId);
        }
        if (type!=null){
            params.put("type", type);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<EnterpriseDeposit> list = enterpriseDepositService.selectList(params);
        PageResult<EnterpriseDeposit> pageResult = new PageResult<EnterpriseDeposit>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条名厨档案
     *
     * @return
    */
    @ApiOperation("查询单条名厨档案")
    @GetMapping("/find/{id}")
    public CommResult<EnterpriseDeposit> find(@PathVariable String id) {
        EnterpriseDeposit result = enterpriseDepositService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("enterpriseDeposit",result);

        Enterprise enterprise = enterpriseService.selectByPrimaryKey(result.getEnterpriseId());
        data.put("enterprise",enterprise);


        return CommResult.success(data);

    }


    /**
     * 人工打款接口
     *
     * @return
     */
    @ApiOperation("人工打款接口")
    @GetMapping("/transfer")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "amount", value = "打款金额", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "tranSn", value = "凭证号", dataType = "String", paramType = "query")
    })
    public CommResult<EnterpriseDeposit> transfer(String id, BigDecimal amount, String tranSn) {
        EnterpriseDeposit result = enterpriseDepositService.findById(id);
        SysEmployee employee = sysEmployeeService.getCurrent();
        if (employee==null) {
            return CommResult.error("无效员工号");
        }
        if (amount.setScale(2,BigDecimal.ROUND_HALF_DOWN).compareTo(result.getDebit().setScale(2,BigDecimal.ROUND_HALF_DOWN))!=0) {
            return CommResult.error("打款金额不正确");
        }
        result.setTranSn(tranSn);
        result.setOperator(employee.getName());
        result.setStatus(2);
        enterpriseDepositService.updateByPrimaryKeySelective(result);


        return CommResult.success(result);

    }


}
